﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MusicStore.Models;
using MusicStore.Storage;

namespace MusicStore.Controllers
{
    [Authorize(Roles = "Administrator")]
    public class StoreManagerController : Controller
    {
        private StoreDbContext db = new StoreDbContext(Settings.ConnectionString);

        //
        // GET: /StoreManager/
        public ActionResult Index()
        {
            var albums = db.Albums.Include("Genre").Include("Artist");

            return View(albums.ToList());
        }

        //
        // GET: /StoreManager/Details/5
        public ActionResult Details(int id)
        {
            var album = db.Albums.Include("Genre").Include("Artist").Where(a => a.Id == id).FirstOrDefault();
            
            return View(album);
        }

        //
        // GET: /StoreManager/Create
        public ActionResult Create()
        {
            ViewBag.GenreId = new SelectList(db.Genres, "Id", "Name");
            ViewBag.ArtistId = new SelectList(db.Artists, "Id", "Name");

            return View();
        } 

        //
        // POST: /StoreManager/Create
        [HttpPost]
        public ActionResult Create(Album album)
        {
            try
            {
                if (!ModelState.IsValid)
                    throw new ArgumentException();

                db.Albums.Add(album);
                db.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                ViewBag.GenreId = new SelectList(db.Genres, "Id", "Name");
                ViewBag.ArtistId = new SelectList(db.Artists, "Id", "Name");

                return View(album);
            }
        }
        
        //
        // GET: /StoreManager/Edit/5
        public ActionResult Edit(int id)
        {
            var album = db.Albums.Find(id);

            ViewBag.GenreId = new SelectList(db.Genres, "Id", "Name", album.GenreId);
            ViewBag.ArtistId = new SelectList(db.Artists, "Id", "Name", album.ArtistId);

            return View(album);
        }

        //
        // POST: /StoreManager/Edit/5
        [HttpPost]
        public ActionResult Edit(Album album)
        {
            try
            {
                if (!ModelState.IsValid)
                    throw new ArgumentException();

                db.Entry(album).State = EntityState.Modified;
                db.SaveChanges();
 
                return RedirectToAction("Index");
            }
            catch
            {
                ViewBag.GenreId = new SelectList(db.Genres, "Id", "Name");
                ViewBag.ArtistId = new SelectList(db.Artists, "Id", "Name");

                return View(album);
            }
        }

        //
        // GET: /StoreManager/Delete/5
        public ActionResult Delete(int id)
        {
            var album = db.Albums.Include("Genre").Include("Artist").Where(a => a.Id == id).FirstOrDefault();

            return View(album);
        }

        //
        // POST: /StoreManager/Delete/5
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            try
            {
                var album = db.Albums.Find(id);
                
                db.Albums.Remove(album);
                db.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View(id);
            }
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
